package com.dayuanit.dy15.atm.controller;

import com.dayuanit.dy15.atm.dto.ResponseDTO;
import com.dayuanit.dy15.atm.entity.User;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
public class UserController extends BaseController {

    public UserController() {
        System.out.println("UserController()");
    }

    @Autowired
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/user/login.do")
    public ResponseDTO login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        try {
            //认证 也就是证明你是你
            User user = userService.login(username, password);

            //如果认证通过，那么就将你的信息保存到session对象里。
            //之所以保存到session对象中，是因为session对象生命周期比较长
            HttpSession session = request.getSession();
            session.setAttribute(login_flag, user);

            return ResponseDTO.sucess();
        } catch (BizException be) {
            be.printStackTrace();
            return ResponseDTO.fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.fail("系统错误，请联系客服");
        }


    }

    @RequestMapping("/user/logout.do")
    public ResponseDTO logout(HttpServletRequest request) {
        //如果通过jsessionId找不到session对象，则不创建
        HttpSession session = request.getSession(false);
        if (null == session) {
            return ResponseDTO.sucess();
        }

        //移除掉session里的元素
        session.removeAttribute(login_flag);

        //将session置为无效
        session.invalidate();
        return ResponseDTO.sucess();
    }

    @RequestMapping("/user/register.do")
    public ResponseDTO register(String username, String password, String confirmPassword) {
        userService.register(username, password, confirmPassword);
        return ResponseDTO.sucess();
    }

    @RequestMapping("/user/resetPassword.do")
    public ResponseDTO resetPassword(String oldPassword, String newPassword, String confirmPassword, HttpServletRequest request) {
        try {
            userService.resetPassword(oldPassword, newPassword, confirmPassword, getUserId(request));
            return ResponseDTO.sucess();
        } catch (BizException be) {
            be.printStackTrace();
            return ResponseDTO.fail(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.fail("系统错误，请联系客服");
        }

    }

}
